const router = require("express").Router();
const http = require("http");
const util = require("util");

// 由于cloudnode是https,远程api是http
/*
jquery.min.js:4 Mixed Content: The page at 'https://bdpost-v1.cloudno.de/#about/track' was loaded over HTTPS, but requested an insecure script 'http://oms.euasia.eu/api/track/json/99?callback=jQuery32105210387555043319_1502902700951&_=1502902700952'. This request has been blocked; the content must be served over HTTPS.
*/
// 因此做了这个api来代理
router.get("/:no", function (req, res, next) {
    // debugger
    var trackno = req.params['id'];
    var query = req.query;
    const REMOTE_API_SERVER = "http://oms.euasia.eu/api/track/json";
    var full_uri = REMOTE_API_SERVER + req.url;
    // res.setHeader("X-DEBUG", util.inspect([req.url, req.baseUrl, req.originalUrl]));
    /*
    "[ '/eaxcp1750039?callback=jQuery32109133452382555203_1518622717613&_=1518622717616',
    '/api',
    '/api/eaxcp1750039?callback=jQuery32109133452382555203_1518622717613&_=1518622717616' ]"
    */
    http.get(full_uri, function (httpResponse) {
        res.setHeader("content-type", httpResponse.headers['content-type']);
        httpResponse.pipe(res);
    });
})

module.exports = router;